package com.yonyou.pmclouds.repeatwork.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.*;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.picture.entity.PictureParamVO;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForDeleteProcessor;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForInsertProcessor;
import com.yonyou.pmclouds.repeatwork.entity.RepeatWorkVO;
import com.yonyou.pmclouds.repeatwork.mapper.RepeatWorkMapper;
import com.yonyou.pmclouds.repeatwork.process.CaculateStopDaysProcessor;
import com.yonyou.pmclouds.repeatwork.process.CheckWorkStop4ProjectProcessor;
import com.yonyou.pmclouds.repeatwork.process.RepeatWorkCopyPersonInsertBatchProcessor;
import com.yonyou.pmclouds.repeatwork.process.RepeatWorkLogPostProcess;
import com.yonyou.pmclouds.repeatwork.service.rmiitf.IRepeatWorkMaintain;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workflow.service.rmiitf.IFlowApproveBuziService;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version= ApplicationConsts.APPLICATION_VERSION, interfaceClass = IRepeatWorkMaintain.class)
public class RepeatWorkServiceImpl implements IRepeatWorkMaintain , IFlowApproveBuziService {
    private static String[] UPDATE_FIELDS ={RepeatWorkVO.PROJECT_NAME,RepeatWorkVO.PROJECT_CODE,RepeatWorkVO.REPEATDATE,RepeatWorkVO.REPEATREASON};
    private static String[] UPDATE_STATUS = {RepeatWorkVO.BILLSTATUS};

    @Autowired
    private RepeatWorkMapper mapper;
    @Autowired
    private PictureRemoteService pictureRemoteService;
    @Autowired
    private ProjectStatusCheckProcessor<RepeatWorkVO> projectStatusCheckProcessor;
    @Autowired
    private RepeatWorkLogPostProcess repeatWorkLogPostProcess;
    @Autowired
    private MobileTaskInsertProcessor<RepeatWorkVO> taskInsertProcessor;//待办
    @Autowired
    private MobileNoticeInsertProcessor<RepeatWorkVO> mobileNoticeInsertProcessor;//抄送人发通知
    @Autowired
    private RepeatWorkCopyPersonInsertBatchProcessor repeatWorkCopyPersonInsertBatchProcessor; //抄送人
    /*@Autowired
    private BillDrivenProcessorFactory drivenProcessorFactory;*/
    @Autowired
    private UpdateBillCountForDeleteProcessor.UpdateBillCountForDeleteProcessorFactory<RepeatWorkVO> billCountDeleteFactory;
    @Autowired
    private CheckWorkStop4ProjectProcessor checkWorkStop4ProjectProcessor;
    @Autowired
    private CaculateStopDaysProcessor caculateStopDaysProcessor;
    @Autowired
    private UpdateBillCountForInsertProcessor.UpdateBillCountForInsertProcessorFactory<RepeatWorkVO> billCountFactory;


    @Override
    @PMApprove
    public RepeatWorkVO add(RepeatWorkVO repeatWorkVO) throws BusinessException {
        if ( repeatWorkVO == null ){
            return null;
        }
        //复工单数据持久化
        SingleInsertBpTemplate<RepeatWorkVO> bp = new SingleInsertBpTemplate<RepeatWorkVO>(mapper);
        // 更新审计信息
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<RepeatWorkVO>());
        // 复工单是基于项目未关闭的情况下
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        //校验是否有相应的停工单
        bp.addBeforeProcessor(checkWorkStop4ProjectProcessor);
        //更新单据数量统计
        bp.addAfterProcessor(billCountFactory.getProcessor(BillTypeConst.REPEATWORK_BILLTYPE));
        //智能日志
        bp.addAfterProcessor(repeatWorkLogPostProcess);
        //抄送人
        bp.addAfterProcessor(repeatWorkCopyPersonInsertBatchProcessor);
        //待办  （自动发送）
//        bp.addAfterProcessor(taskInsertProcessor);
        bp.insert(repeatWorkVO);
        //复工单的图片或视频保存
        PictureParamVO paramVO = new PictureParamVO(repeatWorkVO.getPkRepeatwork(),repeatWorkVO.getPkProject(), BillTypeConst.REPEATWORK_BILLTYPE);
        pictureRemoteService.insertPictureForPub(repeatWorkVO,paramVO);

        return repeatWorkVO;
    }

    @Override
    @PMApprove(trigger = "commit")
    public RepeatWorkVO update(RepeatWorkVO repeatWorkVO) throws BusinessException {
        SingleUpdateBpTemplate<RepeatWorkVO> bp = new SingleUpdateBpTemplate<RepeatWorkVO>(mapper,UPDATE_FIELDS);
        //校验是否有相应的停工单
        bp.addBeforeProcessor(checkWorkStop4ProjectProcessor);
        // 更新审计信息
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<RepeatWorkVO>());
        // 复工单是基于项目未关闭，可进行业务的情况下
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        return bp.update(repeatWorkVO);
    }

    @Override
    public RepeatWorkVO approve(String pkRepeatwork, byte billStatus) throws BusinessException {
        if (StringUtil.isEmpty(pkRepeatwork)){
            return null;
        }
        RepeatWorkVO repeatWorkVO = mapper.getById(pkRepeatwork);
        SingleUpdateBpTemplate<RepeatWorkVO> bp= new SingleUpdateBpTemplate<RepeatWorkVO>(mapper,UPDATE_STATUS);
        repeatWorkVO.setBillStatus(billStatus);
        bp.addBeforeProcessor(caculateStopDaysProcessor);
        //单据驱动项目状态改变
//        bp.addAfterProcessor(drivenProcessorFactory.getBillDrivenProcessor(BillTypeConst.REPEATWORK_BILLTYPE, (BillDrivenProcessorFactory.AdditionalInfoToDriven<RepeatWorkVO>) null));


        //修改复工单
        bp.update(repeatWorkVO);

        return repeatWorkVO;
    }

    @Override
    public RepeatWorkVO reject(String pkRepeatwork) throws BusinessException {
        if (StringUtil.isEmpty(pkRepeatwork)){
            return null;
        }
        RepeatWorkVO repeatWorkVO = mapper.getById(pkRepeatwork);
        SingleUpdateBpTemplate<RepeatWorkVO> bp= new SingleUpdateBpTemplate<RepeatWorkVO>(mapper,UPDATE_STATUS);
        repeatWorkVO.setBillStatus(BillStatusConst.COMMON_REJECT_STATUS);
        // 复工单是基于项目未关闭的情况下
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        //单据驱动项目状态改变
//        bp.addAfterProcessor(drivenProcessorFactory.getBillDrivenProcessor(BillTypeConst.REPEATWORK_BILLTYPE, (BillDrivenProcessorFactory.AdditionalInfoToDriven<RepeatWorkVO>) null));

        //修改复工单
        bp.update(repeatWorkVO);
        return repeatWorkVO;
    }

    @Override
    public SuperVO approveByPkBill(String pk_bill, byte billStatus) throws BusinessException {
        return approve(pk_bill,billStatus);
    }

    @Override
    public SuperVO rejectByPkBill(String pk_bill) throws BusinessException {
        return reject(pk_bill);
    }

    @Override
    public SuperVO queryDetailForWorkFlow(String pk_bill) throws BusinessException {
        RepeatWorkVO repeatWorkVO = mapper.getById(pk_bill);
        return repeatWorkVO;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.REPEATWORK_BILLTYPE;
    }

    @Override
    public SuperVO deleteBill(SuperVO businessVO) throws BusinessException {
        SingleDeleteBpTemplate<RepeatWorkVO> deleteBpTemplate = new SingleDeleteBpTemplate<RepeatWorkVO>(mapper);
        //更新单据
        deleteBpTemplate.addAfterProcessor(billCountDeleteFactory.getProcessor(BillTypeConst.REPEATWORK_BILLTYPE));
        return deleteBpTemplate.delete((RepeatWorkVO) businessVO);
    }

    @Override
    public Object getMapper() {
        return mapper;
    }
}
